package org.footballmanager.pandafootball.system.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.footballmanager.pandafootball.common.constants.Constants;
import org.footballmanager.pandafootball.system.domain.FootTeam;
import org.footballmanager.pandafootball.system.domain.FootUser;
import org.footballmanager.pandafootball.system.domain.FootZj;
import org.footballmanager.pandafootball.system.domain.enums.BFLX;
import org.footballmanager.pandafootball.system.form.impl.FootZjSearchForm;
import org.footballmanager.pandafootball.system.mapper.FootZjMapper;
import org.footballmanager.pandafootball.system.service.FootTeamService;
import org.footballmanager.pandafootball.system.service.FootUserService;
import org.footballmanager.pandafootball.system.service.FootZjService;
import org.footballmanager.pandafootball.system.service.FootZnxService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;

import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;
import java.text.DecimalFormat;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class FootZjServiceImpl extends ServiceImpl<FootZj> implements FootZjService {
	private static final double WIN = 10D;
	private static final double SQUARE = 8D;
	private static final double LOSE = 6D;

	private static final double TWIN = 10D;
	private static final double TSQUARE = 8D;
	private static final double TLOSE = 6D;

	private static final double RANKWIN = 3D;
	private static final double RANKSQUARE = 2D;
	private static final double RANKLOSE = 1D;

    @Autowired
    private FootZjMapper footZjMapper;
	@Autowired
	private FootUserService footUserService;
	@Autowired
	private FootTeamService footTeamService;
	@Autowired
	private FootZnxService footZnxService;
 
	@Override
	@Transactional
	public void getMoney(Long qy1, Long qy2, int type, boolean isadd, BFLX bflx, HttpServletRequest request) {
		//获取主队和客队的球队信息
		FootTeam zhuteam = footTeamService.getTeamByUserid(qy1);
		FootTeam keteam = footTeamService.getTeamByUserid(qy2);
		FootUser zhuuser = footUserService.get(qy1);
		FootUser keuser = footUserService.get(qy2);
		double zhuVipLevel = footUserService.getVipLevel(zhuuser, request);
		double keVipLevel = footUserService.getVipLevel(keuser, request);
		if(zhuteam != null && keteam != null){
			//添加资金变动记录
			Double zhuMoney = (type == 0 ? WIN  : type == 1 ? SQUARE : LOSE) * zhuVipLevel;
			Double keMoney = (type == 0 ? LOSE : type == 1 ? SQUARE : WIN) * keVipLevel;
			Double zhuTopMoney = (type == 0 ? TLOSE : type == 1 ? TSQUARE : TWIN) * zhuVipLevel;
			Double zhuRankMoney = (type == 0 ? RANKWIN  : type == 1 ? RANKSQUARE : RANKLOSE) * zhuVipLevel;
			Double keRankMoney = (type == 0 ? RANKLOSE  : type == 1 ? RANKSQUARE : RANKWIN) * keVipLevel;
			if(bflx.equals(BFLX.NORMAL)){
				this.updateTeamMoney(zhuuser, zhuteam, zhuMoney, isadd, bflx, request);
				this.updateTeamMoney(keuser, keteam, keMoney, isadd, bflx, request);
			}else if(bflx.equals(BFLX.TOP)){
				this.updateTeamMoney(zhuuser, zhuteam, zhuTopMoney, isadd, bflx, request);
			}else{
				this.updateTeamMoney(zhuuser, zhuteam, zhuRankMoney, isadd, bflx, request);
				this.updateTeamMoney(keuser, keteam, keRankMoney, isadd, bflx, request);
			}
		}
	}

	private void updateTeamMoney(FootUser user, FootTeam team, double money, boolean isadd, BFLX bflx, HttpServletRequest request) {
		String resultString = isadd ? "获得" : "失去";
		FootZj zj = new FootZj();
		zj.setUserId(user.getId());
		zj.setUserName(user.getNike());
		zj.setBgsj(new Date());
		team.setQdZzj(team.getQdZzj() + (isadd ? money : 0D - money));
		zj.setBgzj(money);
		zj.setBgsm(user.getNike() + resultString + "了" + bflx.name() + "奖金€" + 
				new DecimalFormat(",###").format(money) + ",当前球队的总资金为:€" + 
				new DecimalFormat(",###").format(team.getQdZzj()));
		footTeamService.updateNotNull(team, request);
		this.save(zj, request);
	}

	@Override
	@Transactional
	public void sendZj(Long userId, Double jg, boolean isz, String bdsm, HttpServletRequest request){
		FootZj zj = new FootZj();
		if(isz){
			zj.setBgzj(jg);
		}else{
			zj.setBgzj(0 - jg);
		}
		zj.setBgsj(new Date());
		FootUser user = footUserService.get(userId);
		if(null != user){
			zj.setUserId(userId);
			zj.setUserName(user.getNike());
		}
		zj.setBgsm(bdsm);
		this.save(zj, request);
	}

	@Override
	public void getMissionMoney(Long userId, Double money, Boolean isReshen, HttpServletRequest request) {
		//任务奖励发放
		String userName = footUserService.get(userId).getNike();
		FootTeam team = footTeamService.getTeamByUserid(userId);
		if(null != team){
			String result = userName + "完成任务" + (isReshen ? "1" : "2") + ",获得奖励" + money + "PB";
			team.setQdZzj(team.getQdZzj() + money);
			footTeamService.updateNotNull(team, request);
			this.sendZj(userId, money, true, result, request);
			footZnxService.sendZnx(footUserService.getAdminUserid(), userId, result, null, null, request);
		}
	}

    /**
     * 翻页方法
     *
     * @param footZjSearchForm 条件
     * @return 列表
     */
    @Override
    public List<FootZj> findall(FootZjSearchForm footZjSearchForm) {
        return footZjMapper.findall(footZjSearchForm);
    }

	/**
	 * 根据主键获取记录
	 * @param id 主键
	 * @return 记录
	 */
	@Override
	public FootZj get(Serializable id) {
		return footZjMapper.findById(id);
	}

    /**
     * 查询
     *
     * @param entityname       实体类名
     * @param map              参数
     * @param footZjSearchForm 条件
     */
    @Override
    public void search(String entityname, Model map, FootZjSearchForm footZjSearchForm) {
        map.addAttribute(String.format(Constants.FORMNAME, entityname), footZjSearchForm);
        Page<FootZj> page = PageHelper.startPage(footZjSearchForm.getPageIndex(), footZjSearchForm.getPageSize());
        List<FootZj> list = this.findall(footZjSearchForm);
        map.addAttribute(Constants.PI, page.toPageInfo());
        map.addAttribute(String.format(Constants.LISTENTITY, entityname), list);
    }

    /**
     * 删除所有
     *
     * @param footZjSearchForm 条件
     * @param request          请求
     */
    @Override
    public void deleteAll(FootZjSearchForm footZjSearchForm, HttpServletRequest request) {
        if(footZjSearchForm.getSelectIds() != null){
            String[] result = footZjSearchForm.getSelectIds().split(Constants.COMMA);
            for(String id: result){
                this .delete(Long.parseLong(id), request);
            }
        }
    }
}
